import Vue from 'vue'
import VueRouter from 'vue-router'
import A from '../components/A.vue'
import AA from '../components/AA.vue'
import AAA from '../components/AAA.vue'
import B from '../components/B.vue'
import Login from '../components/Login.vue'
import Welcome from '../components/Welcome.vue'
import Dept from '../components/Dept.vue'
import Emp from '../components/Emp.vue'
import Parent from '../components/Parent.vue'


Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '*',
    redirect: '/a'
  },
  {
    path: '/a',
    name: 'a',
    component: A,
    children: [{
      path: 'aa',
      name: 'aa',
      component: AA,
      children: [{
        path: 'aaa',
        name: 'aaa',
        component: AAA,
      }]
    }]
  },
  {
    path: '/b',
    name: 'b',
    component: B
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/welcome',
    name: 'welcome',
    component: Welcome,
    children: [
      {
        path: 'emp',
        name: 'emp',
        component: Emp,
      },
      {
        path: 'dept',
        name: 'dept',
        component: Dept,
      }]
  },
  {
    path: '/parent',
    component: Parent,
  },
]

const router = new VueRouter({
  routes,
  //mode: "hash"  //hash:默认’hash’值，但是hash看起来就像无意义的字符排列，
  //不太好看也不符合我们一般的网址浏览习惯。

  mode: "history"   //hash url 跳转histroy:当你使用 history 模式时，URL 就像正常的 url，
  //例如 http://localhost:8080/hi，也好看！
})

// to:我要去的地址
// from:我来之哪里
// next:放行
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next();
  }
  const token = localStorage.getItem("token");
  console.log("token:" + token);
  if (token == 'null' || token == null || token == '') {
    return next('/login');
  }
  next();
})

router.afterEach((to, from) => {
  // console.log(to);
  // console.log(from);
})

export default router
